package com.cloudptt.api.product.func.call.group.fsm.video;

import com.cloudptt.api.product.log.Log;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.GroupMembInfo;

import java.io.Serializable;


public class GroupCallMemberInfo implements Serializable,Comparable<GroupCallMemberInfo>{
	private GroupMembInfo addrInfo;
	private String jsonStr = "";
	private boolean isTrans = false;
	private boolean isRecv = false;

	public GroupCallMemberInfo(GroupMembInfo addrInfo, String jsonStr)
	{
		this.addrInfo = addrInfo;
		this.jsonStr = jsonStr;
		if(!jsonStr.equals("")
			&& addrInfo != null)
		{
			addrInfo.setStatus(GroupMembInfo.Status_Connected);
		}
	}

	public boolean isNull()
	{
		if(addrInfo == null
			&& jsonStr.equals(""))
		{
			return true;
		}
		return false;
	}

//	public void update(AddrInfo addrInfo, String jsonStr)
//	{
//		this.addrInfo = addrInfo;
//		this.jsonStr = jsonStr;
//	}

	public void updateRecv(boolean isRecv)
	{
		this.isRecv = isRecv;
	}

	public void updateTrans( boolean isTrans)
	{
		this.isTrans = isTrans;
	}

	public void update( boolean isTrans, boolean isRecv)
	{
		Log.debug(this.getClass(),"update isTrans =  "+ isTrans + " isRecv =  "+ isRecv);
		this.isTrans = isTrans;
		this.isRecv = isRecv;
		if(addrInfo != null)
		{
			addrInfo.setStatus(GroupMembInfo.Status_Connected);
		}
		Log.debug(this.getClass(),"update addrInfo =  "+ addrInfo);
	}

	public GroupMembInfo getGroupMembInfo() {
		return addrInfo;
	}

	public String getJsonStr() {
		return jsonStr;
	}

	public boolean isTrans() {
		return isTrans;
	}
	public boolean isSameOne(GroupCallMemberInfo ob) {
		if(ob != null
			&& ob.getGroupMembInfo() != null
			&& addrInfo != null
			&& addrInfo.isSameOne(ob.getGroupMembInfo()))
		{
			return true;
		}
		return false;
	}

	public boolean isSameOne(String addrId) {
		Log.debug(this.getClass(),"isSameOne addrId =  "+ addrId + " addrInfo =  "+ addrInfo);
		if(addrId == null)
		{
			Log.debug(this.getClass(),"isSameOne addrId == null");
		}
		if(addrInfo == null)
		{
			Log.debug(this.getClass(),"isSameOne addrInfo == null");
		}
		boolean isSameOne = addrInfo.isSameOne(addrId);
		Log.debug(this.getClass(),"isSameOne isSameOne = " + isSameOne);
		if(addrId != null
				&& addrInfo != null
				&& addrInfo.isSameAddrId(addrId))
		{
			return true;
		}
		else
		{
			Log.debug(this.getClass(),"no isSameOne");
		}
		return false;
	}

	public boolean isRecv() {
		return isRecv;
	}
	@Override
	public int compareTo(GroupCallMemberInfo other) {
		int trans = Boolean.compare(other.isTrans,this.isTrans);
		int recv = Boolean.compare(other.isRecv,this.isRecv);
		int val =  other.addrInfo.getStatus() - this.addrInfo.getStatus();

		int result = getCompareResult(trans,recv,val);
		Log.debug(this.getClass(),"compareTo result = " + result);
		return result;
	}

	private int getCompareResult(int trans,int recv,int val)
	{
		Log.debug(this.getClass(),"getCompareResult trans =  "+ trans + " recv =  "+ recv + " val = " + val);
		if(trans != 0)
		{
			return trans;
		}
		else
		{
			if(recv != 0)
			{
				return recv;
			}
			else
			{
				return val;
			}
		}
	}
	public String toString()
	{
		StringBuffer strBuff = new StringBuffer("GroupCallMemberInfo:");
		strBuff.append("addrInfo = ").append(addrInfo).append(",");
		strBuff.append("isTrans = ").append(isTrans).append(",");
		strBuff.append("isRecv = ").append(isRecv).append(",");
		strBuff.append("jsonStr = ").append(jsonStr).append(".");
		return strBuff.toString();
	}
}
